

#include "ydlidar_GS2.h"

#include "usart.h"


#define PRINTF_CHAR_MAX  256


/*!
    \brief      usart0 initialize configure
    \param[in]  none
    \param[out] none
    \retval     none
*/

void usart0_init(void)
{
	rcu_periph_clock_enable(RCU_USART0);
	
	#ifdef GD32F303_64PIN

	// configure usart gpio.
	gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);  
	gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_10);  
	
	#else
	
	gpio_pin_remap_config(GPIO_USART0_REMAP, ENABLE);
	
	gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6);  
	gpio_init(GPIOB, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_7);  

	
	#endif

	// configure usart register
	usart_deinit(USART0);                                          // usart reset
	usart_word_length_set(USART0, USART_WL_8BIT);                  // word length 8Bit
	usart_stop_bit_set(USART0, USART_STB_1BIT);                    // stop bit 1Bit
	usart_parity_config(USART0, USART_PM_NONE);
	usart_baudrate_set(USART0, UART0_BAUDRATE);                    // baudrate configure
	usart_receive_config(USART0, USART_RECEIVE_ENABLE);            // enable usart recive
	usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);          // enable usart transmit
	usart_hardware_flow_rts_config(USART0, USART_RTS_DISABLE);
	usart_hardware_flow_cts_config(USART0, USART_CTS_DISABLE);
	usart_enable(USART0);                                          // enable usart

	// configure usart interrput
	nvic_irq_enable(USART0_IRQn, 0, 1);

	#if   0
	usart_interrupt_enable(USART0, USART_INT_RBNE);   // enable Uart receive interrput
	#elif 1
	usart_interrupt_enable(USART0, USART_INT_IDLE);   // enable Uart Idle interrput
	usart_interrupt_enable(USART0, USART_INT_ERR);    // enable Uart Erro interrput
	
	#endif 
}


/*!
    \brief      USART0_DATA Handle CallBack
    \param[in]  none
    \param[out] none
    \retval     none
*/
void USART0_DMA0_CH4_CallBack(void)
{
	static int16_t dma_CHCNT = 0;
	int16_t dma_tran_num = 0;
	int16_t dma_tran_len = 0;

	// cal DMA recive data length
	dma_tran_num = dma_transfer_number_get(DMA0, DMA_CH4);
	
	dma_tran_len = dma_CHCNT - dma_tran_num;
	userUARTRecbuf.writeIndex += dma_tran_len;
	userUARTRecbuf.writeIndex %= userUARTRecbuf.databufLen;

	// store DMA tran cnt now
	dma_CHCNT = dma_tran_num;
}


/*!
    \brief      usart0 interrput service routine
    \param[in]  none
    \param[out] none
    \retval     none
*/

void USART0_IRQHandler(void)
{
	if(usart_interrupt_flag_get(USART0, USART_INT_FLAG_IDLE) != RESET)
	{
		// clear usart IDLE interrput flag
		usart_interrupt_flag_clear(USART0, USART_INT_FLAG_IDLE);
		usart_data_receive(USART0);

		USART0_DMA0_CH4_CallBack();
		
		#if 0
		if((set_systemtime_flg != 0) && (frame_analysis_protect == 0))
		{
			interrupt_analysis_flg = 1;
			comm_frame_analysis(cmdframGroup, &userUARTRecbuf);
			userHdl.assis.commcnt = 0;
		}
		#endif
	}
	
    else if(usart_flag_get(USART0, USART_FLAG_ORERR ) ||   \
			usart_flag_get(USART0, USART_FLAG_NERR  ) ||   \
			usart_flag_get(USART0, USART_FLAG_FERR  )   )
	{
		volatile uint32_t tmp;
		
        tmp = USART_STAT0(USART0);
		tmp = USART_DATA(USART0);           
	}
}

/*!
    \brief      UART_PrintfMesg
    \param[in]  none
    \param[out] none
    \retval     none
*/

int16_t UART_PrintfMesg(const char* format, ...)
{
    static char logMessage[PRINTF_CHAR_MAX];
    va_list     arg;
    int32_t     sizeMessage;

    va_start (arg, format);
    sizeMessage = vsnprintf (&logMessage[0], sizeof(logMessage), format, arg);
    va_end (arg);

    return ringBufWrite(&userUARTSendbuf, logMessage, sizeMessage);
}


/*!
    \brief      UART_PrintfMesg
    \param[in]  none
    \param[out] none
    \retval     none
*/


int16_t debug_PrintfMesg(const char* format, ...)
{
    static char logMessage[PRINTF_CHAR_MAX];
    va_list     arg;
    int32_t     sizeMessage;

    va_start (arg, format);
    sizeMessage = vsnprintf (&logMessage[0], sizeof(logMessage), format, arg);
    va_end (arg);

    return ringBufWrite(&printfUARTSendbuf, logMessage, sizeMessage);
}


/*!
    \brief      UART_PrintfMesg
    \param[in]  none
    \param[out] none
    \retval     none
*/

int16_t UART1_PrintfMesg(const char* format, ...)
{
    static char logMessage[PRINTF_CHAR_MAX];
    va_list     arg;
    int32_t     sizeMessage;

    va_start (arg, format);
    sizeMessage = vsnprintf (&logMessage[0], sizeof(logMessage), format, arg);
    va_end (arg);

    return ringBufWrite(&UART1Sendbuf, logMessage, sizeMessage);
}



/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

void usart1_init(void)
{
	rcu_periph_clock_enable(RCU_GPIOD);
	rcu_periph_clock_enable(RCU_USART1);
	
	gpio_pin_remap_config(GPIO_USART1_REMAP, ENABLE);
	
	gpio_init(GPIOD, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_5);  
	gpio_init(GPIOD, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ, GPIO_PIN_6);  
	
	// configure usart register
	usart_deinit(USART1);                                          // usart reset
	usart_word_length_set(USART1, USART_WL_8BIT);                  // word length 8Bit
	usart_stop_bit_set(USART1, USART_STB_1BIT);                    // stop bit 1Bit
	usart_parity_config(USART1, USART_PM_NONE);
	usart_baudrate_set(USART1, UART1_BAUDRATE);                    // baudrate configure
	usart_receive_config(USART1, USART_RECEIVE_ENABLE);            // enable usart recive
	usart_transmit_config(USART1, USART_TRANSMIT_ENABLE);          // enable usart transmit
	usart_hardware_flow_rts_config(USART1, USART_RTS_DISABLE);
	usart_hardware_flow_cts_config(USART1, USART_CTS_DISABLE);
	usart_enable(USART1);                                          // enable usart

	// configure usart interrput
	nvic_irq_enable(USART1_IRQn, 0, 0);
	

	#if   0
	usart_interrupt_enable(USART0, USART_INT_RBNE);   // enable Uart receive interrput
	#elif 1
	usart_interrupt_enable(USART1, USART_INT_IDLE);   // enable Uart Idle interrput
	usart_interrupt_enable(USART1, USART_INT_ERR);    // enable Uart Idle interrput
	#endif 
}



/*!
    \brief      USART1_DATA Handle CallBack
    \param[in]  none
    \param[out] none
    \retval     none
*/
void USART1_DMA0_CH5_CallBack(void)
{
	static int16_t dma_CHCNT = 0;
	int16_t dma_tran_num = 0;
	int16_t dma_tran_len = 0;

	// cal DMA recive data length
	dma_tran_num = dma_transfer_number_get(DMA0, DMA_CH5);
	
	dma_tran_len = dma_CHCNT - dma_tran_num;
	UART1Recbuf.writeIndex += dma_tran_len;
	UART1Recbuf.writeIndex %= UART1Recbuf.databufLen;

	// store DMA tran cnt now
	dma_CHCNT = dma_tran_num;
	
	#if 0
	gs2Hdl[0].data_end_cnt = 0;
	#endif
}


/*!
    \brief      usart2 interrput service routine
    \param[in]  none
    \param[out] none
    \retval     none
*/

void USART1_IRQHandler(void)
{		
	if(usart_interrupt_flag_get(USART1, USART_INT_FLAG_IDLE) != RESET)
	{
		// clear usart IDLE interrput flag
		usart_interrupt_flag_clear(USART1, USART_INT_FLAG_IDLE);
		usart_data_receive(USART1);

		USART1_DMA0_CH5_CallBack();
	}
	
	else if(usart_interrupt_flag_get(USART1, USART_INT_FLAG_TC) != RESET)
	{
		EN_RS485_UART1_RECE();
		usart_interrupt_flag_clear(USART1, USART_INT_FLAG_TC);
		usart_interrupt_disable(USART1, USART_INT_TC); 
	}
	
	else
	{
		volatile uint32_t tmp;
		
		tmp = USART_STAT0(USART1);
		tmp = USART_DATA(USART1);	
	}
}

/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

void usart2_init(void)
{
	rcu_periph_clock_enable(RCU_GPIOC);
	rcu_periph_clock_enable(RCU_GPIOD);
	rcu_periph_clock_enable(RCU_USART2);
	
	#if 0
	gpio_pin_remap_config(GPIO_USART2_PARTIAL_REMAP, ENABLE);

	gpio_init(GPIOC, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_10);  
	gpio_init(GPIOC, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_11); 
	
	#else
	gpio_pin_remap_config(GPIO_USART2_FULL_REMAP, ENABLE);

	gpio_init(GPIOD, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_8);  
	gpio_init(GPIOD, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ, GPIO_PIN_9); 
	
	#endif
	
	// configure usart register
	usart_deinit(USART2);                                          // usart reset
	usart_word_length_set(USART2, USART_WL_8BIT);                  // word length 8Bit
	usart_stop_bit_set(USART2, USART_STB_1BIT);                    // stop bit 1Bit
	usart_parity_config(USART2, USART_PM_NONE);
	usart_baudrate_set(USART2, UART2_BAUDRATE);                    // baudrate configure
	usart_receive_config(USART2, USART_RECEIVE_ENABLE);            // enable usart recive
	usart_transmit_config(USART2, USART_TRANSMIT_ENABLE);          // enable usart transmit
	usart_hardware_flow_rts_config(USART2, USART_RTS_DISABLE);
	usart_hardware_flow_cts_config(USART2, USART_CTS_DISABLE);
	usart_enable(USART2);                                          // enable usart

	// configure usart interrput
	nvic_irq_enable(USART2_IRQn, 0, 0);

	#if   0
	usart_interrupt_enable(USART0, USART_INT_RBNE);   // enable Uart receive interrput
	#elif 1
	usart_interrupt_enable(USART2, USART_INT_IDLE);   // enable Uart Idle interrput
	usart_interrupt_enable(USART2, USART_INT_ERR);    // enable Uart Idle interrput
	#endif 
}



/*!
    \brief      USART2_DATA Handle CallBack
    \param[in]  none
    \param[out] none
    \retval     none
*/
void USART2_DMA0_CH2_CallBack(void)
{
	static int16_t dma_CHCNT = 0;
	int16_t dma_tran_num = 0;
	int16_t dma_tran_len = 0;

	// cal DMA recive data length	
	dma_tran_num = dma_transfer_number_get(DMA0, DMA_CH2);
	
	dma_tran_len = dma_CHCNT - dma_tran_num;
	UART2Recbuf.writeIndex += dma_tran_len;
	UART2Recbuf.writeIndex %= UART2Recbuf.databufLen;

	// store DMA tran cnt now
	dma_CHCNT = dma_tran_num;
	
	#if 0
	gs2Hdl[1].data_end_cnt = 0;
	#endif
}




/*!
    \brief      usart2 interrput service routine
    \param[in]  none
    \param[out] none
    \retval     none
*/

void USART2_IRQHandler(void)
{	
	if(usart_interrupt_flag_get(USART2, USART_INT_FLAG_IDLE) != RESET)
	{
		// clear usart IDLE interrput flag
		usart_interrupt_flag_clear(USART2, USART_INT_FLAG_IDLE);
		usart_data_receive(USART2);

		USART2_DMA0_CH2_CallBack();
	}
	else
	{
		#if 1
		volatile uint32_t tmp;
		
        tmp = USART_STAT0(USART2);
		tmp = USART_DATA(USART2);
		#endif
	}
}



/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

void usart3_init(void)
{
	rcu_periph_clock_enable(RCU_GPIOC);
	rcu_periph_clock_enable(RCU_UART3);
	
	gpio_init(GPIOC, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_10);  
	gpio_init(GPIOC, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_11); 
		
	// configure usart register
	usart_deinit(UART3);                                          // usart reset
	usart_word_length_set(UART3, USART_WL_8BIT);                  // word length 8Bit
	usart_stop_bit_set(UART3, USART_STB_1BIT);                    // stop bit 1Bit
	usart_parity_config(UART3, USART_PM_NONE);
	usart_baudrate_set(UART3, UART3_BAUDRATE);                    // baudrate configure
	usart_receive_config(UART3, USART_RECEIVE_ENABLE);            // enable usart recive
	usart_transmit_config(UART3, USART_TRANSMIT_ENABLE);          // enable usart transmit
	usart_hardware_flow_rts_config(UART3, USART_RTS_DISABLE);
	usart_hardware_flow_cts_config(UART3, USART_CTS_DISABLE);
	usart_enable(UART3);                                          // enable usart

	// configure usart interrput
	nvic_irq_enable(UART3_IRQn, 1, 1);

	#if   0
	usart_interrupt_enable(UART3, USART_INT_RBNE);   // enable Uart receive interrput
	#elif 1
	usart_interrupt_enable(UART3, USART_INT_IDLE);   // enable Uart Idle interrput
	usart_interrupt_enable(UART3, USART_INT_ERR);    // enable Uart Erro interrput
	#endif 
}




/*!
    \brief      USART3_DATA Handle CallBack
    \param[in]  none
    \param[out] none
    \retval     none
*/
void UART3_DMA0_CH2_CallBack(void)
{
	static int16_t dma_CHCNT = 0;
	int16_t dma_tran_num = 0;
	int16_t dma_tran_len = 0;

	// cal DMA recive data length	
	dma_tran_num = dma_transfer_number_get(DMA1, DMA_CH2);
	
	dma_tran_len = dma_CHCNT - dma_tran_num;
	printfUARTRecbuf.writeIndex += dma_tran_len;
	printfUARTRecbuf.writeIndex %= printfUARTRecbuf.databufLen;

	// store DMA tran cnt now
	dma_CHCNT = dma_tran_num;
}




/*!
    \brief      usart3 interrput service routine
    \param[in]  none
    \param[out] none
    \retval     none
*/

void UART3_IRQHandler(void)
{	
    if(usart_interrupt_flag_get(UART3, USART_INT_FLAG_IDLE) != RESET)
	{
		// clear usart IDLE interrput flag
		usart_interrupt_flag_clear(UART3, USART_INT_FLAG_IDLE);
		usart_data_receive(UART3);
		
		UART3_DMA0_CH2_CallBack();
	}
	else
	{
		#if 1
		volatile uint32_t tmp;
		
        tmp = USART_STAT0(UART3);
		tmp = USART_DATA(UART3);
		#endif
	}
}


/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

void usart4_init(void)
{
	rcu_periph_clock_enable(RCU_GPIOC);
	rcu_periph_clock_enable(RCU_GPIOD);
	rcu_periph_clock_enable(RCU_UART4);
	
	gpio_init(GPIOC, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_12);  
	gpio_init(GPIOD, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_2); 
		
	// configure usart register
	usart_deinit(UART4);                                          // usart reset
	usart_word_length_set(UART4, USART_WL_8BIT);                  // word length 8Bit
	usart_stop_bit_set(UART4, USART_STB_1BIT);                    // stop bit 1Bit
	usart_parity_config(UART4, USART_PM_NONE);
	usart_baudrate_set(UART4, UART4_BAUDRATE);                    // baudrate configure
	usart_receive_config(UART4, USART_RECEIVE_ENABLE);            // enable usart recive
	usart_transmit_config(UART4, USART_TRANSMIT_ENABLE);          // enable usart transmit
	usart_hardware_flow_rts_config(UART4, USART_RTS_DISABLE);
	usart_hardware_flow_cts_config(UART4, USART_CTS_DISABLE);
	usart_enable(UART4);                                          // enable usart

	// configure usart interrput
	nvic_irq_enable(UART4_IRQn, 1, 1);

	#if   1
	usart_interrupt_enable(UART4, USART_INT_RBNE);   // enable Uart receive interrput
	#elif 0
	usart_interrupt_enable(UART4, USART_INT_IDLE);   // enable Uart Idle interrput
	usart_interrupt_enable(UART4, USART_INT_ERR);    // enable Uart Idle interrput
	#endif 
}



/*!
    \brief      usart4 interrput service routine
    \param[in]  none
    \param[out] none
    \retval     none
*/

#if 1
void UART4_IRQHandler(void)
{
	// send data -----------------------------------
	if(usart_interrupt_flag_get(UART4, USART_INT_FLAG_TBE) != RESET)
	{
		
		
		usart_interrupt_flag_clear(UART4, USART_INT_FLAG_TC);
		
		usart_interrupt_disable(UART4, USART_INT_TBE);
		
		
			
	}
	
	// receive data -------------------------------------
	else if(usart_interrupt_flag_get(UART4, USART_INT_FLAG_RBNE) != RESET)
	{

		usart_interrupt_flag_clear(UART4, USART_INT_FLAG_RBNE);
	}
	
	else
	{
		#if 1
		volatile uint32_t temp;

		temp = USART_STAT0(UART4);
		temp = USART_DATA(UART4);
		#endif
	}
}
#endif

